#include "net_pan.h"
#include "work.h"
#include "tool.h"
int path_handle(char* curpwd,char* rootpwd,message_t* message,char* str);
int work(int netfd,struct sockaddr_in* netaddr){
    //接受结构体
    char curpwd[1024];
    char rootpwd[1024];
    char lastpwd[1024];
    bzero(curpwd,sizeof(curpwd));
    bzero(rootpwd,sizeof(rootpwd));
    bzero(lastpwd,sizeof(lastpwd));
    char ID[1024];
    //将ID初始值设置为IP+端口号
    char str[1024];
    bzero(str,sizeof(str));
    
    message_t message;
    bzero(&message,sizeof(message));
    int ret=recv(netfd,&message,sizeof(message_t),MSG_WAITALL);
    ERROR_CHECK(ret,-1,"worker_recv");
    //针对不同命令进行处理
    while(message.order!=EXIT){
        //判断用户是否登陆过
        if(strlen(curpwd)==0&&message.order!=ID_PASSWORD){
            //返回请登录信息
            train_t train;
            train.length=-1;
            strcpy(train.data,"Please log first!");
            send(netfd,&train,sizeof(train),0);
            continue;
        }
        switch(message.order){
            case ID_PASSWORD:
                ret=ID_PASSWORD_handle(netfd,curpwd,&message);
                if(ret==0){
                    strcpy(rootpwd,curpwd);
                    strcpy(lastpwd,curpwd);
                    strcpy(ID,message.path2);
                    SYSLOG(ID,"LOG IN","/","success");
                }else{
                    SYSLOG("ID","LOG IN","NULL","fail");
                }
                break;
            case CD:
                ret=CD_handle(ID,netfd,&message,curpwd,rootpwd,lastpwd);
                if(ret!=-1){
                    PWD_handle(netfd,curpwd);
                }else{
                    train_t train;
                    train.length=-1;
                    send(netfd,&train,sizeof(train),0);
                }
                break;
            case LS:
                LS_handle(ID,curpwd,netfd);
                break;
            case PUTS:
                path_handle(curpwd,rootpwd,&message,str);
                if(str[strlen(str)-1]!='/'){
                    strcat(str,"/");
                }
                strcat(str,message.path2);
                PUTS_handle(ID,netfd,str);
                break;
            case GETS:
                path_handle(curpwd,rootpwd,&message,str);
                if(str[strlen(str)-1]!='/'){
                    strcat(str,"/");
                }
                strcat(str,message.path2);
                printf("str = %s\n",str);
                GETS_handle(ID,netfd,str);
                break;
            case REMOVE:
                path_handle(curpwd,rootpwd,&message,str);
                strcat(str,message.path2);
                REMOVE_handle(netfd,ID,str);
                break;
            case PWD:
                PWD_handle(netfd,curpwd);
                SYSLOG(ID,"PWD",curpwd,"success");
                break;
            case MKDIR:
                path_handle(curpwd,rootpwd,&message,str);
                strcat(str,message.path2);
                printf("str = %s\n",str);
                MKDIR_handle(ID,str,netfd);
                break;
            case RMDIR:
                path_handle(curpwd,rootpwd,&message,str);
                strcat(str,message.path2);
                printf("str = %s\n",str);
                if(strcmp(str,lastpwd)==0){
                    strcpy(lastpwd,rootpwd);
                }
                RMDIR_handle(ID,str,netfd);
                break;
        }
        bzero(&message,sizeof(message));
        ret=recv(netfd,&message,sizeof(message_t),MSG_WAITALL);
        ERROR_CHECK(ret,-1,"worker_recv");
    }
    //客户端退出处理
    SYSLOG(ID,"EXIT","NULL","success");;
    train_t train;
    train.length=0;
    send(netfd,&train,sizeof(train),0);
    close(netfd);//优化
    return 0;
}
int path_handle(char* curpwd,char* rootpwd,message_t* message,char *str){
    printf("path1 = %s\n",message->path1);
    printf("path2 = %s\n",message->path2);
    //将传来的目标路径转换为绝对路径，路径格式错误返回-1
    //不允许./和../单独出现！
    if(message->path1[0]=='.'&&message->path1[1]=='/'){
        //从当前目录出发
        strcpy(str,curpwd);
        strcat(str,&message->path1[1]);
    }else if(message->path1[0]=='/'){
        //从绝对路径出发
        strcpy(str,rootpwd);
        strcat(str,message->path1);
        strcat(str,"/");
    }else if(message->path1[0]!='.'&&message->path1[0]!='/'){
        strcpy(str,curpwd);
        strcat(str,"/");
        strcat(str,message->path1);
    }else{// ../
        if(strcmp(curpwd,rootpwd)==0){//没有上一级
            strcpy(str,rootpwd);
            strcat(str,&message->path1[1]);
            return 0;
        }
        int i=strlen(curpwd);
        while(curpwd[i]!='/'){
            i--;
        }
        curpwd[i]='\0';
        strcpy(str,curpwd);
        strcat(str,&message->path1[2]);
    }
    return 0;
}

//int char_div(char*str,char**result){
//    int i=0;
//    while(str[i]!=' '){
//        i++;
//    }
//    str[i]='\0';
//    strcpy(result[0],str);
//    strcpy(result[1],&str[i+1]);
//    return 0;
//}
